package com.example.rbacmanage.service.impl;

import com.example.rbacmanage.entity.Area;
import com.example.rbacmanage.exception.MyException;
import com.example.rbacmanage.mapper.AreaMapper;
import com.example.rbacmanage.service.AreaService;
import com.example.rbacmanage.vo.AreaTreeResp;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class AreaServiceImpl implements AreaService {
    @Autowired
    private AreaMapper areaMapper;
    @Override
    public void save(Area area) {
        if (area.getParentId() == null || area.getParentId() == 0L) {
            area.setLevel(0);
        }else{
            Area parent = areaMapper.queryOneById(area.getParentId());
            if (parent != null) {
                area.setLevel(parent.getLevel()+1);
            }
        }
        area.setCreateTime(new Date());
        areaMapper.insert(area);
    }

    @Override
    public void remove(Long[] ids) {
        for (Long id:ids){
            areaMapper.del(id);
        }
    }

    @Override
    public void update(Area area) {
        if (area.getParentId() == null) {
            area.setLevel(0);
        }else{
            Area parent = areaMapper.queryOneById(area.getParentId());
            if (parent != null) {
                area.setLevel(parent.getLevel()+1);
            }
        }
        areaMapper.update(area);
    }

    @Override
    public List<Area> getAll(Area area) {
        List<Area> list=areaMapper.list(area);
        List<Long> parentIds = list.stream().map(Area::getParentId).distinct().toList();
        if (!Collections.isEmpty(parentIds)) {
            Area a = new Area();
            a.setIds(parentIds);
            List<Area> parents = areaMapper.list( a);
            for (Area area1:list){
                for (Area p:parents){
                    if (area1.getParentId().equals(p.getId())){
                        area1.setParentName(p.getAreaName());
                        break;
                    }
                }
            }

        }
        return list;
    }

    @Override
    public PageInfo<Area> getPage(Area area) {
        PageHelper.startPage(area.getPage(),area.getLimit());
        return new PageInfo<>(getAll(area));
    }

    @Override
    public Area getById(Long id) {
        return areaMapper.queryOneById(id);
    }

    @Override
    public Integer getCount(Area area) {
        return areaMapper.count(area);
    }

    @Override
    public void checkRepeat(Area area) throws MyException {

    }

    @Override
    public List<AreaTreeResp> tree(Area area) {
        List<Area> list = getAll(area);
        List<AreaTreeResp> res = new ArrayList<>();
        list.forEach(a->{
                AreaTreeResp resp = new AreaTreeResp();
                resp.setLabel(a.getAreaName());
                resp.setText(a.getAreaName());
                resp.setValue(a.getId());
                resp.setParentId(a.getParentId());
                resp.setParentName(a.getParentName());
                resp.setId(a.getId());
                resp.setAreaName(a.getAreaName());
                resp.setLevel(a.getLevel());
                resp.setCreateTime(a.getCreateTime());
                res.add(resp);
        });
        return buildTreeSelect(res);
    }

    private List<AreaTreeResp> buildTreeSelect(List<AreaTreeResp> resps) {
        List<AreaTreeResp> returnList = new ArrayList<>();
        List<Long> tempList = resps.stream().map(AreaTreeResp::getId).toList();
        for (AreaTreeResp resp:resps){
            if (!tempList.contains(resp.getParentId())){
                recursionFn(resps,resp);
                returnList.add(resp);
            }
        }
        if (returnList.isEmpty()){
            return resps;
        }
        return returnList;
    }

    //递归列表
    private void recursionFn(List<AreaTreeResp> deptList, AreaTreeResp areaTreeResp) {
        //得到子节点列表
        List<AreaTreeResp> childList = getChildList(deptList,areaTreeResp);
        areaTreeResp.setChildren(childList);
        for (AreaTreeResp child : childList){
            //是否有子孩子
            boolean hasChild = getChildList(deptList,child).size() > 0;
            if (hasChild){
                recursionFn(deptList,child);
            }
        }
    }
    //得到子节点列表
    public List<AreaTreeResp> getChildList(List<AreaTreeResp> deptList,AreaTreeResp areaTreeResp){
        List<AreaTreeResp> childList = new ArrayList<>();
        Iterator<AreaTreeResp> it = deptList.iterator();
        while (it.hasNext()){
            AreaTreeResp n = it.next();
            if (n.getParentId() != null && n.getParentId().equals(areaTreeResp.getId())){
                childList.add(n);
            }
        }
        return childList;
    }
}
